﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using EntityObject;
using SharedLibraries;
using ThangNM_LIB.FUNC;

namespace DataAccessObject
{
    public class TopicDAO : DB_SQL_EXT
    {
        public Boolean Insert_Update_Delete(ref TopicEO _TopicEO, Int16 Insert_Update_Delete)
        {
            SqlCommand cmd = new SqlCommand("Topic_Insert_Update_Delete");
            cmd.CommandType = CommandType.StoredProcedure;
            //Insert tblSEO
            setParamSEO(_TopicEO, ref cmd);

            cmd.Parameters.Add(new SqlParameter("@FK_sFileID", NullEx.getNullValue(_TopicEO.FK_sFileID)));
            cmd.Parameters.Add(new SqlParameter("@FK_iAccountID", NullEx.getNullValue(_TopicEO.FK_iAccountID)));
            cmd.Parameters.Add(new SqlParameter("@sContent", NullEx.getNullValue(_TopicEO.sContent)));
            cmd.Parameters.Add(new SqlParameter("@Insert_Update_Delete", Insert_Update_Delete));
            _TopicEO.PK_iSEO = ExecuteNonQuery_RetInt32(cmd);
            if (_TopicEO.PK_iSEO > 0)
                return true;
            else
                return false;
        }

        public Boolean Update_iVisit_Or_iLike(TopicEO _TopicEO)
        {
            SqlCommand cmd = new SqlCommand("Topic_Update_iVisit_Or_iLike");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@PK_iTopicID", _TopicEO.PK_iTopicID));
            //cmd.Parameters.Add(new SqlParameter("@iVisit", NullEx.getNullValue(_TopicEO.iVisit)));
            //cmd.Parameters.Add(new SqlParameter("@iLike", NullEx.getNullValue(_TopicEO.iLike)));
            return ExecuteNonQuery_RetBool(cmd);
        }

        public TopicEO SelectItem(Int32 _PK_iTopicID)
        {
            SqlCommand cmd = new SqlCommand("Topic_SelectItem");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@PK_iTopicID", _PK_iTopicID));
            return TopicDO(GetDataTable(cmd));
        }

        public DataTable SelectBy_iState(Int16 _iState)
        {
            SqlCommand cmd = new SqlCommand("Topic_SelectBy_iState");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@iState", NullEx.getNullValue(_iState)));
            return GetDataTable(cmd);
        }

        public DataTable SelectList_Rss(Int32 _iTop)
        {
            SqlCommand cmd = new SqlCommand("Topic_SelectList_Rss");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@iTop", _iTop));
            return GetDataTable(cmd);
        }

        public DataSet SelectList_Sitemap()
        {
            SqlCommand cmd = new SqlCommand("Topic_SelectList_Sitemap");
            cmd.CommandType = CommandType.StoredProcedure;
            return GetDataSet(cmd);
        }

        public DataSet SelectBy_PK_iTopicID(Int32 _PK_iTopicID)
        {
            SqlCommand cmd = new SqlCommand("Topic_SelectBy_PK_iTopicID");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@PK_iTopicID", _PK_iTopicID));
            return GetDataSet(cmd);
        }

        public DataTable SelectBy_FK_iAccountID(Int32 _FK_iAccountID)
        {
            SqlCommand cmd = new SqlCommand("Topic_SelectBy_FK_iAccountID");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@FK_iAccountID", _FK_iAccountID));
            return GetDataTable(cmd);
        }

        public DataSet getDataMasterPage()
        {
            SqlCommand cmd = new SqlCommand("getDataMasterPage");
            cmd.CommandType = CommandType.StoredProcedure;
            return GetDataSet(cmd);
        }

        public DataSet getDataChart(Int16 iType)
        {
            SqlCommand cmd = new SqlCommand("getChart");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@iType", iType));
            return GetDataSet(cmd);
        }

        public DataTable getNotification()
        {
            SqlCommand cmd = new SqlCommand("getNotification");
            cmd.CommandType = CommandType.StoredProcedure;
            return GetDataTable(cmd);
        }

        public TopicEO TopicDO(DataTable input)
        {
            List<TopicEO> lst = new List<TopicEO>();
            lst = (List<TopicEO>)ConvertEx.ConvertDataTableToListObject<TopicEO>(input, lst.GetType());
            if (CommonsCheck.ListObject_OK(lst))
                return lst[0];
            else
                return null;
        }
    }
}